package cn.iocoder.yudao.module.reading.controller.admin.category;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.reading.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.reading.convert.category.CategoryConvert;
import cn.iocoder.yudao.module.reading.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.reading.service.category.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 绘本分类
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 绘本分类")
@RestController("adminCategoryController")
@RequestMapping("/reading/admin/categories")
@Validated
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本分类")
    @PreAuthorize("@ss.hasPermission('reading:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody CategoryCreateReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本分类")
    @PreAuthorize("@ss.hasPermission('reading:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody CategoryUpdateReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reading:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reading:category:query')")
    public CommonResult<CategoryRespVO> getCategory(@RequestParam("id") Long id) {
        CategoryDO category = categoryService.getCategory(id);
        return success(CategoryConvert.INSTANCE.convertAdmin(category));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本分类分页")
    @PreAuthorize("@ss.hasPermission('reading:category:query')")
    public CommonResult<PageResult<CategoryRespVO>> getCategoryPage(@Valid CategoryPageReqVO pageReqVO) {
        PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(pageReqVO);
        return success(CategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @Operation(summary = "获得绘本分类列表")
    @PreAuthorize("@ss.hasPermission('reading:category:query')")
    public CommonResult<List<CategoryRespVO>> getCategoryList(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "language", required = false) String language) {
        List<CategoryDO> list;
        if (language != null && !language.isEmpty()) {
            list = categoryService.getActiveCategoriesByLanguage(language);
        } else {
            list = categoryService.getActiveCategories();
        }
        return success(CategoryConvert.INSTANCE.convertAdminList(list));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得绘本分类精简列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<CategorySimpleRespVO>> getSimpleCategoryList(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "language", required = false) String language) {
        List<CategoryDO> list;
        if (language != null && !language.isEmpty()) {
            list = categoryService.getActiveCategoriesByLanguage(language);
        } else {
            list = categoryService.getActiveCategories();
        }
        return success(CategoryConvert.INSTANCE.convertSimpleList(list));
    }

}
